package 反射;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
        Class<?> aClass = Class.forName("反射.Student");
//        Class<Student> aClass1 = Student.class;
//        Student student  = new Student();
//        Class<? extends Student> aClass2 = student.getClass();
//        System.out.println(aClass == aClass1);
//        System.out.println(aClass2 == aClass1);
//        Method[] methods = aClass.getDeclaredMethods();
//        Constructor<?>[] constructors = aClass.getConstructors();
//        for (Constructor constructor:constructors
//             ) {
//            System.out.println(constructor);
//        }
//        System.out.println("++++++++++++++");
//        Constructor<?>[] constructors1 = aClass.getDeclaredConstructors();
//        for (Constructor constructor:constructors1
//        ) {
//            System.out.println(constructor);
//        }
//        System.out.println("=================");
//        for (Method m :methods) {
//            System.out.println(m);
//        }
//        Constructor<?> declaredConstructor = aClass.getDeclaredConstructor(int.class,String.class,String.class);
//        Student student = (Student) declaredConstructor.newInstance(100,"李四","男");
//        System.out.println(student);
//        Constructor<Student> declaredConstructor = (Constructor<Student>) aClass.getDeclaredConstructor(int.class);
//       //通过setAccessible()方法破坏私有方法的封装性。
//        declaredConstructor.setAccessible(true);
//        Student student = declaredConstructor.newInstance(100);
//        System.out.println(student);
//        System.out.println(declaredConstructor);
//        Method fun = aClass.getDeclaredMethod("fun");
//        fun.setAccessible(true);
//        fun.invoke(aClass.newInstance());
//        Method func = aClass.getDeclaredMethod("func", String.class);
//        func.invoke(null,"123");
        Field tall = aClass.getDeclaredField("tall");
        System.out.println(tall.get(null));
        Student o = (Student) aClass.newInstance();
        Student student = new Student(19,"lihua","女");
        Field name = aClass.getDeclaredField("name");
        System.out.println(name.get(student));
        name.set(student,"lili");
        System.out.println(name.get(student));
    }
}
